package com.quanxiaoha.xiaohashu.note.biz.controller;

import com.quanxiaoha.framework.biz.operationlog.aspect.ApiOperationLog;
import com.quanxiaoha.framework.common.response.Response;
import com.quanxiaoha.xiaohashu.note.biz.domain.dataobject.ChannelDO;
import com.quanxiaoha.xiaohashu.note.biz.domain.mapper.ChannelDOMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/channel")
@Slf4j
public class ChannelController {

    @Resource
    private ChannelDOMapper channelDOMapper;

    @GetMapping(value = "/list")
    @ApiOperationLog(description = "获取频道列表")
    public Response<?> getChannelList() {
        try {
            log.info("开始查询频道列表...");
            
            // 从数据库查询所有未删除的频道
            List<ChannelDO> channelDOS = channelDOMapper.selectAllChannels();
            log.info("查询到频道数量: {}", channelDOS.size());
            
            // 转换为前端需要的格式
            List<Map<String, Object>> channels = new ArrayList<>();
            for (ChannelDO channelDO : channelDOS) {
                Map<String, Object> channel = new HashMap<>();
                channel.put("id", channelDO.getId());
                channel.put("name", channelDO.getName());
                channels.add(channel);
                log.info("频道: id={}, name={}", channelDO.getId(), channelDO.getName());
            }
            
            log.info("频道列表查询成功，返回{}个频道", channels.size());
            return Response.success(channels);
            
        } catch (Exception e) {
            log.error("查询频道列表失败", e);
            return Response.fail("查询频道列表失败: " + e.getMessage());
        }
    }
} 